package com.spring.demo.service;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.OOSpider;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.TargetUrl;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.FilePageModelPipeline;

import java.util.List;

/**
 * 注解方式爬虫
 */
@TargetUrl("http://my.oschina.net/flashsword/blog/*")
public class OschinaBlog implements AfterExtractor {

    //用ExtractBy注解的字段会被自动抽取并填充
    //默认是xpath语法
    @ExtractBy("//h1[@class='article-box__title']/a/text()")
    private String title;

    //可以定义抽取语法为Css、Regex等
    @ExtractBy(value = "div.content", type = ExtractBy.Type.Css)
    private String content;

    //multi标注的抽取结果可以是一个List
    @ExtractBy(value = "//div[@class='tags-box__inner']/a/text()", multi = true)
    private List<String> tags;

    @Override
    public void afterProcess(Page page) {
        System.out.println("title:"+ this.title);
//        System.out.println("content:"+ this.content);
        System.out.println("tags:"+ this.tags);
    }

    public static void main(String[] args) {
        //启动webmagic
        OOSpider.create(Site.me(), new FilePageModelPipeline(), OschinaBlog.class).addPipeline(new ConsolePipeline())
                .addUrl("http://my.oschina.net/flashsword/blog/145796").run();
    }
}
